package tyk;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Proxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import tyk.JDK.MyProxyHandler;
import tyk.JDK.UserServicee;
import tyk.JDK.UserServiceeImpll;
import tyk.aop.UserService;
import tyk.cglib.MyMethodInterceptor;
import tyk.cglib.UserServic;
import tyk.config.SpringConfig;

/**
 * Hello world!
 *
 */
public class Main
{
    public static void main( String[] args )
    {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserService userService = context.getBean(UserService.class);
        System.out.println("------------------");
        userService.addUser();
        System.out.println("------------------");
        userService.deleteUser();
        System.out.println("------------------");
        userService.updateUser();
        System.out.println("------------------");
        userService.queryUser();
        System.out.println("------------------");

        UserServicee userServicee = new UserServiceeImpll();
        MyProxyHandler handler = new MyProxyHandler(userServicee);

        UserServicee proxyObject = (UserServicee) Proxy.newProxyInstance(
                UserServicee.class.getClassLoader(),
                new Class<?>[]{UserServicee.class},
                handler
        );

//        // 调用代理对象的方法
//        proxyObject.getUserByID();
//        System.out.println("---------------");
//        // 创建字节码增强器
//        Enhancer enhancer = new Enhancer();
//        // 告诉cglib要继承哪个类
//        enhancer.setSuperclass(UserServic.class);
//        // 设置回调接口
//        enhancer.setCallback(new MyMethodInterceptor());
//        // 生成源码，编译class，加载到JVM，并创建代理对象
//        UserServic userServiceProxy = (UserServic) enhancer.create();
//        // 调用方法
//        userServiceProxy.getUserByID();
    }
}
